package com.spring.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import org.springframework.stereotype.Component;

public class NettyServer {
    private final ServerBootstrap serverBootstrap;
    private final int port;
    private ChannelFuture channelFuture;

    public NettyServer(ServerBootstrap serverBootstrap, int port) {
        this.serverBootstrap = serverBootstrap;
        this.port = port;
    }

    public void start() throws Exception {
        channelFuture = serverBootstrap.bind(port).sync();
        System.out.println("Netty服务器启动，监听端口: " + port);
    }

    public void stop() throws Exception {
        if (channelFuture != null) {
            channelFuture.channel().closeFuture().sync();
        }
    }
}
